<?php
/**
* @Author By Chris
*
* Created on 2011-7-29
*
*/

class ArticleAction extends Action
{
	public function _initialize()//初始化
	{
		$config = D('setting')->getConfig();
		$categoryItem = getCategoryCache();
		$this->assign('categoryItem',$categoryItem);
		$this->assign('site_config',$config);
	}
	public function index()
	{
		$id = $_GET['id'] ? intval($_GET['id']) : '';
		if (empty($id))
			$this->error('文章参数错误!');
		$articleData = M('posts')->where(array('id'=>$id))->find();
		if ($articleData)
		{
			
			self::updateClick($id);//对浏览数量更新
			/*$articleData['pre'] = self::getPreArticle($id);
			$articleData['next'] = self::getNextArticle($id);*/
			$articleData['related'] = self::getRelatedArticle($articleData['cid']);//获取相关文档
			$articleData['tag'] = self::getTag($id);//tag
			$articleData['c_name'] = getArticleCategoryName($articleData['cid']);
			$articleData['commentcount'] = self::getCommentCount($id);//读取评论个数
			$articleData['comment'] = self::getComment($id);//评论
			$this->assign('articleData',$articleData);//所有文章信息
			A('Public')->right();//载入右侧信息
			$this->display();
			
		}
		else
		{
			$this->error('无此文档');
		}
	}
	
	//获取上一篇文章
	protected static function getPreArticle($aid)
	{
		return M('posts')->where("id < '$aid' ")->field('subject,id')->find();
	}
	
	//获取下一篇文章
	protected static function getNextArticle($aid)
	{
		return M('posts')->where("id > '$aid' ")->field('subject,id')->find();
	}
	
	/**
	 * 获取相当文章
	 * @param int $aid //文章ID
	 * @param int $num //文章数目
	 */
	protected static function getRelatedArticle($cid,$num='12')
	{
		return M('posts')->where(array('cid'=>$cid))->limit('0,'.$num)->order('id desc')->field('subject,click_count,createdate,id')->select();
		
	}
	
	//获取评论
	protected static function getComment($aid)
	{
		return M('comment')->where(array('comment_approved'=>'1','comment_post_ID'=>$aid))->order('comment_ID desc')->select();
		
	}
	
	protected static function getTag($aid)
	{
		return D('tag')->getTag($aid);//获取tag信息
	}
	
	protected static function updateClick($aid)
	{
		$data['click_count'] = array('exp','click_count+1');
		M('posts')->where(array('id'=>$aid))->data($data)->save();
	}
	
	//获取当前文档的评论数
	protected static function getCommentCount($aid)
	{
		return M('comment')->where(array('comment_post_ID'=>$aid))->count();
	}
}
?>